package com.onlineMIS.ORM.DAO.headQ.inventoryFlow;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInOutStock;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadQInventoryStock;
import com.onlineMIS.common.Common_util;


@Repository
public class HeadQInventoryStockDAOImpl extends BaseDAO<HeadQInventoryStock> {
	
		
	/**
	 * 根据库房和条码id获取系统库存数量
	 * @param pbId
	 * @param storeId
	 * @return
	 */
	public int getProductStock(int pbId, int storeId, boolean cache){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQInventoryStock.class);
		
		criteria.add(Restrictions.eq("storeId", storeId));
		criteria.add(Restrictions.eq("productBarcode.id", pbId));
		
		criteria.setProjection(Projections.sum("quantity"));
		
		List<Object> result = getByCriteriaProjection(criteria, cache);

        return Common_util.getProjectionSingleValue(result);
	}
	
	
	/**
	 * 根据pbId 获取货品库存数量
	 * @param pbId
	 * @return
	 */
	public int getInventory(int pbId){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQInventoryStock.class);
		
		criteria.add(Restrictions.eq("productBarcode.id", pbId));
		
		criteria.setProjection(Projections.sum("quantity"));
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionSingleValue(result);
	}

	
	public Map<Integer, Integer> getProductsInventoryLevel(Set<Integer> pbIds, int storeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQInventoryStock.class);
		
		criteria.add(Restrictions.in("productBarcode.id", pbIds));
		
		if (storeId != Common_util.ALL_RECORD)
		   criteria.add(Restrictions.eq("storeId", storeId));
		
		ProjectionList projList = Projections.projectionList();
		projList.add(Projections.groupProperty("productBarcode.id"));
		projList.add(Projections.sum("quantity"));
		criteria.setProjection(projList);
		
		List<Object> result = getByCriteriaProjection(criteria, false);
		Map<Integer, Integer> stockMap = new HashMap<Integer, Integer>();
		
		//1. to put the result to stock map
		for (Object object : result)
		  if (object != null){
			Object[] recordResult = (Object[])object;
			int pbId = (Integer)recordResult[0];
			int stock =  (Integer)recordResult[1];
			stockMap.put(pbId, stock);
		  } 

		return stockMap;
	}


	public int getProductStock(String barcode, int clientId, boolean cache){
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadQInventoryStock.class);
		
		criteria.add(Restrictions.eq("barcode", barcode));
		criteria.add(Restrictions.eq("clientId", clientId));
		
		criteria.setProjection(Projections.sum("quantity"));
		
		List<Object> result = getByCriteriaProjection(criteria, cache);

        return Common_util.getProjectionSingleValue(result);
	}
}
